//
//  RGCustomNavBarView.m
//  RedGrass
//
//  Created by wukong on 2018/12/28.
//  Copyright © 2018年 hongcaosp. All rights reserved.
//

#import "RGCustomNavBarView.h"

@interface RGCustomNavBarView()

@property (strong, nonatomic) UILabel *titleLabel;
@property (nonatomic,strong)  UIView *shadowLine;

@end

@implementation RGCustomNavBarView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor blackColor];
        [self initUI];
        [self addMasonry];
        
    }
    return self;
}

-(void) initUI{
    
    [self addSubview:self.titleLabel];
    [self addSubview:self.shadowLine];
}

-(void) addMasonry{
    
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(self).mas_offset(0);
        make.width.mas_equalTo(KScreenWidth - 150);
        make.height.mas_equalTo(44);
        make.bottom.mas_equalTo(self).mas_offset(-2);
    }];
    
    [_shadowLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self).mas_offset(0);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
    
}



#pragma  mark - setter
-(void) setTitle:(NSString *)title{
    if(![title isKindOfClass:[NSString class]]){
        return;
    }
    _title = title;
    _titleLabel.text = SafeStr(title);
}

-(void) setTitleFont:(UIFont *)titleFont{
    if( (titleFont == nil) || ![titleFont isKindOfClass:[UIFont class]]){
        return;
    }
    _titleFont = titleFont;
    _titleLabel.font = titleFont;
}

-(void) setTitleColor:(UIColor *)titleColor{
    if( (titleColor == nil) || ![titleColor isKindOfClass:[UIColor class]]){
        return;
    }
    _titleColor = titleColor;
    _titleLabel.textColor = titleColor;
}

-(void) setIsShowBottomLine:(BOOL)isShowBottomLine{
    _isShowBottomLine = isShowBottomLine;
    _shadowLine.hidden = !isShowBottomLine;
}

-(void) setLeftButton:(UIButton *)leftButton{
    if([leftButton isKindOfClass:[UIButton class]]){
        
        if(_leftButton){
            [_leftButton removeFromSuperview];
        }
        _leftButton = leftButton;
        [self addSubview:leftButton];
        [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
//            make.bottom.mas_equalTo(0);
            make.centerY.mas_equalTo(self.titleLabel.mas_centerY).mas_offset(0);
            make.width.mas_equalTo(60);
            make.height.mas_equalTo(44);
        }];
    }
}

-(void) setRightButtons:(NSArray<UIButton *> *)rightButtons{
    CGFloat offsetY = 10;
    if(rightButtons.count){
        if(_rightButtons.count){
            [self removeRightBtns];
        }
        _rightButtons = rightButtons;
        for (NSInteger index = 0; index < rightButtons.count; index ++) {
            UIButton *rightBtn = rightButtons[index];
            CGRect buttonFrame = rightBtn.frame;
            CGFloat width = buttonFrame.size.width > 0 ? buttonFrame.size.width : 40;
            CGFloat height = buttonFrame.size.height;
            if(height <= 0 || height > 40){
                height = 40;
            }
            [self addSubview:rightBtn];
            [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(0 - offsetY);
//                make.bottom.mas_equalTo(-1);
                make.centerY.mas_equalTo(self.titleLabel.mas_centerY).mas_offset(0);
                make.width.mas_equalTo(width);
                make.height.mas_equalTo(height);
            }];
            offsetY += 10 + width;
        }
    }
}


-(void) removeRightBtns{
    for (UIButton *rightBtn in _rightButtons) {
        [rightBtn removeFromSuperview];
    }
}

#pragma mark - getter
-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.font = FONT(customBoldFontName, FFont_17);
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

-(UIView *)shadowLine{
    if(!_shadowLine){
        _shadowLine = [[UIView alloc]init];
        _shadowLine.backgroundColor = UICOLOR_WITH_RGBINT(0xEFEFF4);
    }
    return _shadowLine;
}

@end
